from artSightWorld.apps.base.models import *
from django.contrib.auth.models import User
from django.shortcuts import render_to_response
from django.core.context_processors import csrf
from artSightWorld.apps.map.views import *
from artSightWorld.apps.photoDisplay.views import *
from artSightWorld.apps.base.forms import *
from django.template import RequestContext

from django.contrib.auth import authenticate, login
from django.contrib.auth import logout
from django.http import HttpResponseRedirect, HttpResponse


def home(request):
	if (request.user.is_authenticated()):
		myPhotographer = Photographer.objects.get(user__id=request.user.pk);
	else :
		myPhotographer = 0
	return render_to_response('base/home.html', {'myPhotographer':myPhotographer}, context_instance=RequestContext(request) )
	
	
	
def myProfile(request):
	if not request.user.is_authenticated():
		return HttpResponseRedirect('/login')
	photographer = Photographer.objects.get(user__id=request.user.pk);
	return render_to_response('base/my_profile.html', {'photographer':photographer, 'myPhotographer':photographer, 'idAlbum':0}, context_instance=RequestContext(request)  )

def profile(request, idPhotographer, idAlbum=0):
	gridType = "displayLastPopular" 
	mapInfo = getMapInfo(idAlbum)
	#photographer = User.objects.get(pk=idPhotographer);
	photographer = Photographer.objects.get(user__id=idPhotographer);
	if (request.user.is_authenticated()):
		myPhotographer = Photographer.objects.get(user__id=request.user.pk);
	else :
		myPhotographer = 0
	if idAlbum == 0:
		fctPagination = 'displayAlbumsList/'+idPhotographer
	else :
		fctPagination = 'displayAlbumPhoto/'+idPhotographer+'/'+idAlbum
	return render_to_response('base/profile.html', {'photographer':photographer, 'myPhotographer':myPhotographer, 'idAlbum':idAlbum, 'fctPagination':fctPagination, 'zoomed':mapInfo["zoomed"], 'zoomLevel':mapInfo["zoomLevel"], 'zoomLong':mapInfo["zoomLong"], 'zoomLat':mapInfo["zoomLat"]}, context_instance=RequestContext(request)  )
	
	
	
def editProfile(request):
	if not request.user.is_authenticated():
		return HttpResponseRedirect('/login')
	#photographer = Photographer.objects.get(user=request.user.id)
	#return render_to_response('base/profile.html', , context_instance=RequestContext(request)  )
	
def mentions(request):
	return render_to_response('base/mentions.html',context_instance=RequestContext(request) )
	
def conditions(request):
	return render_to_response('base/conditions.html', context_instance=RequestContext(request) )
	
def artsightworld(request):
	return render_to_response('base/artsightworld.html', context_instance=RequestContext(request) )
	
def plandusite(request):
	return render_to_response('base/plandusite.html', context_instance=RequestContext(request) )
	
def recherche(request):
	if request.method == 'POST': # If the form has been submitted...
		form = SearchForm(request.POST) # A form bound to the POST data
		if form.is_valid():
			q = Photo.objects.all()
			if (form.cleaned_data['photographer'] != ""):
				q = Photo.objects.filter(album__photographer__username__icontains=form.cleaned_data['photographer'])
			if (form.cleaned_data['title'] != ""):
				q = q.filter(title__icontains=form.cleaned_data['title'])
			if (form.cleaned_data['dateTakenBegin'] != None):
				q = q.filter(dateTaken__gte=form.cleaned_data['dateTakenBegin'])
			if (form.cleaned_data['dateTakenEnd'] != None):
				q = q.filter(dateTaken__lte=form.cleaned_data['dateTakenEnd'])
			if (form.cleaned_data['datePostedBegin'] != None):
				q = q.filter(dateTaken__gte=form.cleaned_data['datePostedBegin'])
			if (form.cleaned_data['datePostedEnd'] != None):
				q = q.filter(dateTaken__lte=form.cleaned_data['datePostedEnd'])
			if (form.cleaned_data['city'] != ""):
				q = Photo.objects.filter(city__name__icontains=form.cleaned_data['city'])
			if (form.cleaned_data['country'] != ""):
				q = q.filter(city__region__country__name__icontains=form.cleaned_data['country'])
			photos = q
			for photo in photos:
				photo.latitude*=100000000
				photo.longitude*=100000000
			pages = getDataPagination(photos, 100, 1)
			cols = createPhotoCols(pages.object_list,100)
			return render_to_response('base/recherche.html', {'form': form, 'columns':cols, 'photos':photos}, context_instance=RequestContext(request) )
		else:
			photos=[]
			pages = getDataPagination(photos, 100, 1)
			cols = createPhotoCols(pages.object_list,100)
			return render_to_response('base/recherche.html', {'form': form, 'columns':cols, 'photos':photos}, context_instance=RequestContext(request) )
	else :
		form = SearchForm()
		photos=[]
		pages = getDataPagination(photos, 100, 1)
		cols = createPhotoCols(pages.object_list,100)
		return render_to_response('base/recherche.html', {'form': form, 'columns':cols, 'photos':photos}, context_instance=RequestContext(request) )

def activate(request):
	return render_to_response('resgistration/activate.html', context_instance=RequestContext(request) )
	
def addPhotographer(request):
	if request.POST:
		username = request.POST['username']
		user = User.objects.get(username=username)
		if request.POST['birthday'] == "" :
			dateBirth = '0001-01-01'
		else :
			dateBirth = request.POST['birthday']
		photographer = Photographer(dateOfBirth=dateBirth, description='', photoProfile='default.jpg', banned=0, website=request.POST['website'], telNumber='', user=user, status_id=0, city=request.POST['country'])
		photographer.save()
		return render_to_response('registration/activate.html')
	else:
		return HttpResponseRedirect('/')
	
def activation_complete(request):
	gridType = "displayLastPopular" 
	return render_to_response('resgistration/activation_complete.html', context_instance=RequestContext(request) )

def profil(request):
	idPhotographe = 1
	return render_to_response('base/profil.html', {'idPhotographer':idPhotographe}, context_instance=RequestContext(request) );
	
def connexion(request):
	if request.POST:
		username = request.POST['username']
		password = request.POST['password']
		user = authenticate(username=username, password=password)
		print user
		if user is not None:
			if user.is_active:
				login(request, user)
				return HttpResponseRedirect('/')
			else:
				return HttpResponseRedirect('/')
		else:
			return HttpResponseRedirect('/')
	else:
		return HttpResponseRedirect('/')

        

def logout_view(request):
	logout(request)
	return HttpResponseRedirect('/')